﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using RemotingInterfaces;

namespace Model.Spells
{
    public class Spellbook
    {
        private List<ISpell> _spells;
        private int _capacity; //

        private const double _price_capacity_multiplier = 60;

        public Spellbook(int capacity)
        {
            _capacity = capacity;
            _spells = new List<ISpell>();
        }

        public int Capacity
        {
            get { return _capacity; }
        }

        //price of spellbook should be based on its capacity
        public int Price
        {
            get { return (int)(_capacity * _price_capacity_multiplier); }
        }

        public bool Add_Spell(ISpell spell)
        {
            var query = from s in _spells
                        where s.Name == spell.Name
                        select s;
                         
            if (query.Count() == 0)
            {
                _spells.Add(spell);
                return true;
            }
            //the principles of Add_Spell ensures that there can only be one spell with the same name already in the spellbook
            else if ((Spell)query.First() < (Spell)spell)
            {
                //add the stronger version of the spell
                _spells.Remove(query.First());
                _spells.Add(spell);
                return true;
            }
            return false;
        }

        public bool Remove_Spell(ISpell spell)
        {
            ISpell remove = null;
            if ((remove = _spells.Find(s => (Spell)s == (Spell)spell)) == null)
            {
                //spell not found
                return false;
            }
            _spells.Remove(remove);
            return true;
        }

        public override string ToString()
        {
            String str = "";
            str += "Spellbook capacity = " + _capacity + "\n";
            foreach (ISpell spell in _spells)
            {
                str += spell.ToString();
            }
            return str;
        }
    }
}
